package com.bdqn.utli;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.junit.Test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
pom.xml
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-core</artifactId>
  <version>4.0.3</version>
</dependency>
*/
public class NoteSend {
    private static final String KEY_ID = "LTAI3wq8PVfyecM4";//密钥id
    private static final String KEY_SECRET = "ScTCxHQDOu6UrAHN37vDg4A8ADnImB";//密钥值
    //LTAI3wq8PVfyecM4	ScTCxHQDOu6UrAHN37vDg4A8ADnImB

    //发送验证码
    public static String commonRpc(String phone,int code){
        String result=null;
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", KEY_ID, KEY_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "授教网");
        request.putQueryParameter("TemplateCode", "SMS_163846203");
        request.putQueryParameter("TemplateParam", "{'code':'"+code+"'}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            result = response.getData();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return result;
    }
    //生成六位验证码
    public static int createCode(){
        int code = (int)((Math.random()*9+1)*100000);
        return code;
    }
    //手机号格式验证
    public static boolean checkPhone(String phone) {
        String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(phone);
        return m.matches();
    }
}